package me.raih.util;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author Raih
 * @Time 2021/2/16  12:27
 *
 * 商品的工具咯
 */
public class WaresUtil {

    public static Map<String,Integer> caseMap(String sellWaresListStr){

        Map<String,Integer> map = new HashMap<>();
        //检查大猪一头
        if(sellWaresListStr == null || sellWaresListStr.length() == 0 ){
            try {
                throw new Exception("小伙子你传错值了");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }else {
            //先去皮
            String newStr = sellWaresListStr.replace("{","").replace("}","");
            //再切开
            String[] split = newStr.split(",");

            for (String s : split) {
                //洗一洗
                String replace = s.replace(" ", "");
                //再切开
                String[] kv = replace.split("=");
                //装起来
                map.put(kv[0],Integer.parseInt(kv[1]));
            }
        }

        //打包
        return map;
    }

    /**
     * 在长字符串中查询单个字符串出现的次数
     * @param longString 长字符串
     * @param single 单个字符串
     * @return 出现次数
     */
    private static int many(String longString,String single){

        int count = 0;
        int index = 0;
        int num = longString.indexOf(single);
        while ((index = longString.indexOf(single)) != -1) {
            count++;
            longString = longString.substring(longString.indexOf(single) + single.length());
        }
        return count;
    }

}
